<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>linkedmap_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_structs_linkedmap_test.js.html">linkedmap_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.structs.LinkedMapTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.structs.LinkedMapTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.structs.LinkedMap&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line21"></a>
<a name="line22"></a>function fillLinkedMap(m) {
<a name="line23"></a>  m.set(&#39;a&#39;, 0);
<a name="line24"></a>  m.set(&#39;b&#39;, 1);
<a name="line25"></a>  m.set(&#39;c&#39;, 2);
<a name="line26"></a>  m.set(&#39;d&#39;, 3);
<a name="line27"></a>}
<a name="line28"></a>
<a name="line29"></a>var someObj = {};
<a name="line30"></a>
<a name="line31"></a>function testLinkedMap() {
<a name="line32"></a>  var m = new goog.structs.LinkedMap();
<a name="line33"></a>  fillLinkedMap(m);
<a name="line34"></a>
<a name="line35"></a>  assertArrayEquals([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], m.getKeys());
<a name="line36"></a>  assertArrayEquals([0, 1, 2, 3], m.getValues());
<a name="line37"></a>}
<a name="line38"></a>
<a name="line39"></a>function testMaxSizeLinkedMap() {
<a name="line40"></a>  var m = new goog.structs.LinkedMap(3);
<a name="line41"></a>  fillLinkedMap(m);
<a name="line42"></a>
<a name="line43"></a>  assertArrayEquals([&#39;b&#39;, &#39;c&#39;, &#39;d&#39;], m.getKeys());
<a name="line44"></a>  assertArrayEquals([1, 2, 3], m.getValues());
<a name="line45"></a>}
<a name="line46"></a>
<a name="line47"></a>function testLruLinkedMap() {
<a name="line48"></a>  var m = new goog.structs.LinkedMap(undefined, true);
<a name="line49"></a>  fillLinkedMap(m);
<a name="line50"></a>
<a name="line51"></a>  assertArrayEquals([&#39;d&#39;, &#39;c&#39;, &#39;b&#39;, &#39;a&#39;], m.getKeys());
<a name="line52"></a>  assertArrayEquals([3, 2, 1, 0], m.getValues());
<a name="line53"></a>
<a name="line54"></a>  m.get(&#39;a&#39;);
<a name="line55"></a>  assertArrayEquals([&#39;a&#39;, &#39;d&#39;, &#39;c&#39;, &#39;b&#39;], m.getKeys());
<a name="line56"></a>  assertArrayEquals([0, 3, 2, 1], m.getValues());
<a name="line57"></a>
<a name="line58"></a>  m.set(&#39;b&#39;, 4);
<a name="line59"></a>  assertArrayEquals([&#39;b&#39;, &#39;a&#39;, &#39;d&#39;, &#39;c&#39;], m.getKeys());
<a name="line60"></a>  assertArrayEquals([4, 0, 3, 2], m.getValues());
<a name="line61"></a>}
<a name="line62"></a>
<a name="line63"></a>function testMaxSizeLruLinkedMap() {
<a name="line64"></a>  var m = new goog.structs.LinkedMap(3, true);
<a name="line65"></a>  fillLinkedMap(m);
<a name="line66"></a>
<a name="line67"></a>  assertArrayEquals([&#39;d&#39;, &#39;c&#39;, &#39;b&#39;], m.getKeys());
<a name="line68"></a>  assertArrayEquals([3, 2, 1], m.getValues());
<a name="line69"></a>
<a name="line70"></a>  m.get(&#39;c&#39;);
<a name="line71"></a>  assertArrayEquals([&#39;c&#39;, &#39;d&#39;, &#39;b&#39;], m.getKeys());
<a name="line72"></a>  assertArrayEquals([2, 3, 1], m.getValues());
<a name="line73"></a>
<a name="line74"></a>  m.set(&#39;d&#39;, 4);
<a name="line75"></a>  assertArrayEquals([&#39;d&#39;, &#39;c&#39;, &#39;b&#39;], m.getKeys());
<a name="line76"></a>  assertArrayEquals([4, 2, 1], m.getValues());
<a name="line77"></a>}
<a name="line78"></a>
<a name="line79"></a>function testGetCount() {
<a name="line80"></a>  var m = new goog.structs.LinkedMap();
<a name="line81"></a>  assertEquals(0, m.getCount());
<a name="line82"></a>  m.set(&#39;a&#39;, 0);
<a name="line83"></a>  assertEquals(1, m.getCount());
<a name="line84"></a>  m.set(&#39;a&#39;, 1);
<a name="line85"></a>  assertEquals(1, m.getCount());
<a name="line86"></a>  m.set(&#39;b&#39;, 2);
<a name="line87"></a>  assertEquals(2, m.getCount());
<a name="line88"></a>  m.remove(&#39;a&#39;);
<a name="line89"></a>  assertEquals(1, m.getCount());
<a name="line90"></a>}
<a name="line91"></a>
<a name="line92"></a>function testIsEmpty() {
<a name="line93"></a>  var m = new goog.structs.LinkedMap();
<a name="line94"></a>  assertTrue(m.isEmpty());
<a name="line95"></a>  m.set(&#39;a&#39;, 0);
<a name="line96"></a>  assertFalse(m.isEmpty());
<a name="line97"></a>  m.remove(&#39;a&#39;);
<a name="line98"></a>  assertTrue(m.isEmpty());
<a name="line99"></a>}
<a name="line100"></a>
<a name="line101"></a>function testSetMaxCount() {
<a name="line102"></a>  var m = new goog.structs.LinkedMap(3);
<a name="line103"></a>  fillLinkedMap(m);
<a name="line104"></a>  assertEquals(3, m.getCount());
<a name="line105"></a>
<a name="line106"></a>  m.setMaxCount(5);
<a name="line107"></a>  m.set(&#39;e&#39;, 5);
<a name="line108"></a>  m.set(&#39;f&#39;, 6);
<a name="line109"></a>  m.set(&#39;g&#39;, 7);
<a name="line110"></a>  assertEquals(5, m.getCount());
<a name="line111"></a>
<a name="line112"></a>  m.setMaxCount(4);
<a name="line113"></a>  assertEquals(4, m.getCount());
<a name="line114"></a>
<a name="line115"></a>  m.setMaxCount(0);
<a name="line116"></a>  m.set(&#39;h&#39;, 8);
<a name="line117"></a>  m.set(&#39;i&#39;, 9);
<a name="line118"></a>  m.set(&#39;j&#39;, 10);
<a name="line119"></a>  assertEquals(7, m.getCount());
<a name="line120"></a>}
<a name="line121"></a>
<a name="line122"></a>function testClear() {
<a name="line123"></a>  var m = new goog.structs.LinkedMap();
<a name="line124"></a>  fillLinkedMap(m);
<a name="line125"></a>  m.clear();
<a name="line126"></a>  assertTrue(m.isEmpty());
<a name="line127"></a>}
<a name="line128"></a>
<a name="line129"></a>function testForEach() {
<a name="line130"></a>  var m = new goog.structs.LinkedMap();
<a name="line131"></a>  fillLinkedMap(m);
<a name="line132"></a>
<a name="line133"></a>  m.forEach(function(val, key, linkedMap) {
<a name="line134"></a>    linkedMap.set(key, val * 2);
<a name="line135"></a>    assertEquals(&#39;forEach should run in provided context.&#39;, someObj, this);
<a name="line136"></a>  }, someObj);
<a name="line137"></a>
<a name="line138"></a>  assertArrayEquals([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], m.getKeys());
<a name="line139"></a>  assertArrayEquals([0, 2, 4, 6], m.getValues());
<a name="line140"></a>}
<a name="line141"></a>
<a name="line142"></a>function testMap() {
<a name="line143"></a>  var m = new goog.structs.LinkedMap();
<a name="line144"></a>  fillLinkedMap(m);
<a name="line145"></a>
<a name="line146"></a>  var result = m.map(function(val, key, linkedMap) {
<a name="line147"></a>    assertEquals(&#39;The LinkedMap object should get passed in&#39;, m, linkedMap);
<a name="line148"></a>    assertEquals(&#39;map should run in provided context&#39;, someObj, this);
<a name="line149"></a>    return key + val;
<a name="line150"></a>  }, someObj);
<a name="line151"></a>
<a name="line152"></a>  assertArrayEquals([&#39;a0&#39;, &#39;b1&#39;, &#39;c2&#39;, &#39;d3&#39;], result);
<a name="line153"></a>}
<a name="line154"></a>
<a name="line155"></a>function testSome() {
<a name="line156"></a>  var m = new goog.structs.LinkedMap();
<a name="line157"></a>  fillLinkedMap(m);
<a name="line158"></a>
<a name="line159"></a>  var result = m.some(function(val, key, linkedMap) {
<a name="line160"></a>    assertEquals(&#39;The LinkedMap object should get passed in&#39;, m, linkedMap);
<a name="line161"></a>    assertEquals(&#39;map should run in provided context&#39;, someObj, this);
<a name="line162"></a>    return val &gt; 2;
<a name="line163"></a>  }, someObj);
<a name="line164"></a>
<a name="line165"></a>  assertTrue(result);
<a name="line166"></a>  assertFalse(m.some(function(val) {return val &gt; 3}));
<a name="line167"></a>
<a name="line168"></a>  assertTrue(m.some(function(val, key) {return key == &#39;c&#39;;}));
<a name="line169"></a>  assertFalse(m.some(function(val, key) {return key == &#39;e&#39;;}));
<a name="line170"></a>}
<a name="line171"></a>
<a name="line172"></a>function testEvery() {
<a name="line173"></a>  var m = new goog.structs.LinkedMap();
<a name="line174"></a>  fillLinkedMap(m);
<a name="line175"></a>
<a name="line176"></a>  var result = m.every(function(val, key, linkedMap) {
<a name="line177"></a>    assertEquals(&#39;The LinkedMap object should get passed in&#39;, m, linkedMap);
<a name="line178"></a>    assertEquals(&#39;map should run in provided context&#39;, someObj, this);
<a name="line179"></a>    return val &lt; 5;
<a name="line180"></a>  }, someObj);
<a name="line181"></a>
<a name="line182"></a>  assertTrue(result);
<a name="line183"></a>  assertFalse(m.every(function(val) {return val &lt; 2}));
<a name="line184"></a>
<a name="line185"></a>  assertTrue(m.every(function(val, key) {return key.length == 1;}));
<a name="line186"></a>  assertFalse(m.every(function(val, key) {return key == &#39;b&#39;;}));
<a name="line187"></a>}
<a name="line188"></a>
<a name="line189"></a>function testPeek() {
<a name="line190"></a>  var m = new goog.structs.LinkedMap();
<a name="line191"></a>  assertEquals(undefined, m.peek());
<a name="line192"></a>  assertEquals(undefined, m.peekLast());
<a name="line193"></a>
<a name="line194"></a>  fillLinkedMap(m);
<a name="line195"></a>  assertEquals(0, m.peek());
<a name="line196"></a>
<a name="line197"></a>  m.remove(&#39;a&#39;);
<a name="line198"></a>  assertEquals(1, m.peek());
<a name="line199"></a>
<a name="line200"></a>  assertEquals(3, m.peekLast());
<a name="line201"></a>
<a name="line202"></a>  assertEquals(3, m.peekValue(&#39;d&#39;));
<a name="line203"></a>  assertEquals(1, m.peek());
<a name="line204"></a>
<a name="line205"></a>  m.remove(&#39;d&#39;);
<a name="line206"></a>  assertEquals(2, m.peekLast());
<a name="line207"></a>}
<a name="line208"></a>
<a name="line209"></a>function testPop() {
<a name="line210"></a>  var m = new goog.structs.LinkedMap();
<a name="line211"></a>  assertEquals(undefined, m.shift());
<a name="line212"></a>  assertEquals(undefined, m.pop());
<a name="line213"></a>
<a name="line214"></a>  fillLinkedMap(m);
<a name="line215"></a>  assertEquals(4, m.getCount());
<a name="line216"></a>
<a name="line217"></a>  assertEquals(0, m.shift());
<a name="line218"></a>  assertEquals(1, m.peek());
<a name="line219"></a>
<a name="line220"></a>  assertEquals(3, m.pop());
<a name="line221"></a>  assertEquals(2, m.peekLast());
<a name="line222"></a>
<a name="line223"></a>  assertEquals(2, m.getCount());
<a name="line224"></a>}
<a name="line225"></a>
<a name="line226"></a>function testContains() {
<a name="line227"></a>  var m = new goog.structs.LinkedMap();
<a name="line228"></a>  fillLinkedMap(m);
<a name="line229"></a>
<a name="line230"></a>  assertTrue(m.contains(2));
<a name="line231"></a>  assertFalse(m.contains(4));
<a name="line232"></a>}
<a name="line233"></a>
<a name="line234"></a>function testContainsKey() {
<a name="line235"></a>  var m = new goog.structs.LinkedMap();
<a name="line236"></a>  fillLinkedMap(m);
<a name="line237"></a>
<a name="line238"></a>  assertTrue(m.containsKey(&#39;b&#39;));
<a name="line239"></a>  assertFalse(m.containsKey(&#39;elephant&#39;));
<a name="line240"></a>  assertFalse(m.containsKey(&#39;undefined&#39;));
<a name="line241"></a>}
<a name="line242"></a>
<a name="line243"></a>function testRemoveNodeCalls() {
<a name="line244"></a>  var m = new goog.structs.LinkedMap(1);
<a name="line245"></a>  m.removeNode = goog.testing.recordFunction(m.removeNode);
<a name="line246"></a>
<a name="line247"></a>  m.set(&#39;1&#39;, 1);
<a name="line248"></a>  assertEquals(&#39;removeNode not called after adding an element&#39;, 0,
<a name="line249"></a>      m.removeNode.getCallCount());
<a name="line250"></a>  m.set(&#39;1&#39;, 2);
<a name="line251"></a>  assertEquals(&#39;removeNode not called after updating an element&#39;, 0,
<a name="line252"></a>      m.removeNode.getCallCount());
<a name="line253"></a>  m.set(&#39;2&#39;, 2);
<a name="line254"></a>  assertEquals(&#39;removeNode called after adding an overflowing element&#39;, 1,
<a name="line255"></a>      m.removeNode.getCallCount());
<a name="line256"></a>
<a name="line257"></a>  m.remove(&#39;3&#39;);
<a name="line258"></a>  assertEquals(&#39;removeNode not called after removing a non-existing element&#39;, 1,
<a name="line259"></a>      m.removeNode.getCallCount());
<a name="line260"></a>  m.remove(&#39;2&#39;);
<a name="line261"></a>  assertEquals(&#39;removeNode called after removing an existing element&#39;, 2,
<a name="line262"></a>      m.removeNode.getCallCount());
<a name="line263"></a>
<a name="line264"></a>  m.set(&#39;1&#39;, 1);
<a name="line265"></a>  m.clear();
<a name="line266"></a>  assertEquals(&#39;removeNode called after clearing the map&#39;, 3,
<a name="line267"></a>      m.removeNode.getCallCount());
<a name="line268"></a>  m.clear();
<a name="line269"></a>  assertEquals(&#39;removeNode not called after clearing an empty map&#39;, 3,
<a name="line270"></a>      m.removeNode.getCallCount());
<a name="line271"></a>
<a name="line272"></a>  m.set(&#39;1&#39;, 1);
<a name="line273"></a>  m.pop();
<a name="line274"></a>  assertEquals(&#39;removeNode called after calling pop&#39;, 4,
<a name="line275"></a>      m.removeNode.getCallCount());
<a name="line276"></a>  m.pop();
<a name="line277"></a>  assertEquals(&#39;removeNode not called after calling pop on an empty map&#39;, 4,
<a name="line278"></a>      m.removeNode.getCallCount());
<a name="line279"></a>
<a name="line280"></a>  m.set(&#39;1&#39;, 1);
<a name="line281"></a>  m.shift();
<a name="line282"></a>  assertEquals(&#39;removeNode called after calling shift&#39;, 5,
<a name="line283"></a>      m.removeNode.getCallCount());
<a name="line284"></a>  m.shift();
<a name="line285"></a>  assertEquals(&#39;removeNode not called after calling shift on an empty map&#39;, 5,
<a name="line286"></a>      m.removeNode.getCallCount());
<a name="line287"></a>
<a name="line288"></a>  m.setMaxCount(2);
<a name="line289"></a>  m.set(&#39;1&#39;, 1);
<a name="line290"></a>  m.set(&#39;2&#39;, 2);
<a name="line291"></a>  assertEquals(&#39;removeNode not called after increasing the maximum map size&#39;, 5,
<a name="line292"></a>      m.removeNode.getCallCount());
<a name="line293"></a>  m.setMaxCount(1);
<a name="line294"></a>  assertEquals(&#39;removeNode called after decreasing the maximum map size&#39;, 6,
<a name="line295"></a>      m.removeNode.getCallCount());
<a name="line296"></a>}
</pre>


</body>
</html>
